using Framework.Save;
using UnityEngine;
using LitJson;

namespace GameCore
{
    public class EntityData : GameData
    {
        public string entityType;
        public ResourceID resourceID;
        public Vector3 position;
        public Quaternion rotation;
        public Vector3 scale;
        public GameData data;
        
        public EntityData(JsonData json) : base(json) { }

        public override JsonData Serialize()
        {
            var json = JsonUtil.NewObject();
            json["entityType"] = entityType;
            json["resourceID"] = resourceID.ToString();
            var transform = JsonUtil.NewArray();
            transform.Add(position.x);
            transform.Add(position.y);
            transform.Add(position.z);
            transform.Add(rotation.x);
            transform.Add(rotation.y);
            transform.Add(rotation.z);
            transform.Add(rotation.w);
            transform.Add(scale.x);
            transform.Add(scale.y);
            transform.Add(scale.z);
            json["transform"] = transform;
            return json;
        }

        public override void Deserialize(JsonData json)
        {
            entityType = (string)json["entityType"];
            resourceID = (string)json["resourceID"];
            var transform = json["transform"];
            position = new Vector3((float)transform[0], (float)transform[1], (float)transform[2]);
            rotation = new Quaternion((float)transform[3], (float)transform[4], (float)transform[5], (float)transform[6]);
            scale = new Vector3((float)transform[7], (float)transform[8], (float)transform[9]);
        }
    }
}